package alt.djudge.frontend.server.servlets.gwt;

import java.util.List;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

import alt.djudge.frontend.client.rpc.DJudgeGWTContestService;
import alt.djudge.frontend.server.datatypes.ContestEntry;
import alt.djudge.frontend.server.datatypes.ContestProblemEntry;
import alt.djudge.frontend.server.datatypes.LanguageEntry;
import alt.djudge.frontend.server.datatypes.ProblemStatementEntry;
import alt.djudge.frontend.server.datatypes.UserEntry;
import alt.djudge.frontend.server.models.ContestLanguagesModel;
import alt.djudge.frontend.server.models.ContestProblemsModel;
import alt.djudge.frontend.server.models.ContestsModel;
import alt.djudge.frontend.server.models.LocalesModel;
import alt.djudge.frontend.server.models.ProblemStatementsModel;
import alt.djudge.frontend.server.models.SubmissionsModel;
import alt.djudge.frontend.shared.dto.contest.ContestInfoDTO;
import alt.djudge.frontend.shared.dto.contest.ContestProblemInfoDTO;
import alt.djudge.frontend.shared.dto.contest.LanguageInfoDTO;
import alt.djudge.frontend.shared.dto.contest.ProblemStatementInfoDTO;


public class DJudgeGWTContestServiceImpl extends DJudgeGwtRpcServlet implements DJudgeGWTContestService
{
	private static final long serialVersionUID = 1L;
	
	@SuppressWarnings("unused")
	private static final Logger log = Logger.getLogger(DJudgeGWTContestServiceImpl.class);

	@Override
	public ContestProblemInfoDTO[] getProblems(Long contestId)
	{
		List<ContestProblemEntry> contestProblems = ContestProblemsModel.getContestEntries(contestId);
		int sz = contestProblems.size();
		ContestProblemInfoDTO[] problemsDTO = new ContestProblemInfoDTO[sz];
		for (int i = 0; i < sz; i++)
		{
			ContestProblemEntry problem = contestProblems.get(i);
			ContestProblemInfoDTO dto = new ContestProblemInfoDTO();
			dto.setId(problem.getId());
			dto.setName(problem.getName());
			dto.setSid(problem.getSid());
			problemsDTO[i] = dto;
		}
		return problemsDTO;
	}

	@Override
	public ProblemStatementInfoDTO[] getStatements(Long contestProblemId)
	{
		ContestProblemEntry contestProblemEntry = ContestProblemsModel.getEntry(contestProblemId);
		Long problemId = contestProblemEntry.getProblemId();
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("problemId", problemId);
		List<ProblemStatementEntry> statements = ProblemStatementsModel.getEntries(params);
		int cnt = statements.size();
		ProblemStatementInfoDTO[] statementsDTO = new ProblemStatementInfoDTO[cnt];
		for (int i = 0; i < cnt; i++)
		{
			ProblemStatementEntry statement = statements.get(i);
			ProblemStatementInfoDTO statementDTO = new ProblemStatementInfoDTO();
			statementDTO.setId(statement.getId());
			statementDTO.setLocaleName(LocalesModel.getEntry(statement.getLocaleId()).getName());
			statementDTO.setProblemId(problemId);
			statementDTO.setStatement(statement.getStatement());
			statementsDTO[i] = statementDTO;
		}
		return statementsDTO;
	}

	@Override
	public LanguageInfoDTO[] getLanguages(Long contestId)
	{
		List<LanguageEntry> languages = ContestLanguagesModel.getContestEntries(contestId);
		int sz = languages.size();
		LanguageInfoDTO[] result = new LanguageInfoDTO[sz];
		for (int i = 0; i < sz; i++)
		{
			LanguageEntry language = languages.get(i);
			LanguageInfoDTO languageDTO = new LanguageInfoDTO();
			languageDTO.setId(language.getId());
			languageDTO.setName(language.getName());
			languageDTO.setSid(language.getSid());
			System.out.println("" + language.getId() + " " + language.getName());
			result[i] = languageDTO;
		}
		return result;
	}

	@Override
	public ContestInfoDTO getContestInfo(Long contestId)
	{
		ContestEntry contest = ContestsModel.getEntry(contestId);
		return contest.asContestInfoDTO();
	}

	@Override
	public int submitSolution(Long contestId, Long contestProblemId,
			Long languageId, String sourceCode)
	{
		UserEntry user = getCurrentUser();
		ContestEntry contest = ContestsModel.getEntry(contestId);
		return contest.submitSolution(user, contestProblemId, languageId, sourceCode);
	}
}
